package com.bgs.service.impl;

import com.bgs.common.R;
import com.alibaba.fastjson.JSON;

import com.bgs.config.publisherDirect.BeforeDirectConfig;
import com.bgs.pojo.BeforeUser;
import com.bgs.mapper.BeforeUserMapper;
import com.bgs.pojo.vo.ZeroReturnGoods;
import com.bgs.pojo.vo.BeforeMessage;
import com.bgs.service.BeforeUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 前台用户表 服务实现类
 * </p>
 *
 * @author Asia
 * @since 2021-09-14
 */
@Service
public class BeforeUserServiceImpl extends ServiceImpl<BeforeUserMapper, BeforeUser> implements BeforeUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private BeforeUserMapper beforeUserMapper;
    @Resource
    private JavaMailSender mailSender;
    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public R zeroBeforeUserDelete2(Integer beforeUserId) {
        boolean b = beforeUserMapper.zeroBeforeUserDelete2(beforeUserId);
        return R.ok().data("zeroBeforeUserDelete2", b);
    }

    @Override
    public R ThreeSend(String email) {
        BeforeUser beforeUser = baseMapper.ThreeSend(email);
        if (beforeUser == null) {
            StringBuilder code = new StringBuilder();
            for (int i = 0; i < 6; i++) {
                code.append((int) (Math.random() * 9));
            }

            // 发送邮件给
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("1261787524@qq.com");
            message.setSubject("验证码信息");
            message.setText("您的验证码是" + JSON.toJSONString(code) + "请于60秒内填写");
            message.setTo(email);
            mailSender.send(message);

            try {
                System.out.println(code);
                redisTemplate.opsForValue().set(email, code, 50, TimeUnit.SECONDS);
                return R.ok().data("code", code);
            } catch (Exception e) {
                e.printStackTrace();
                return R.error().message("发送验证码失败");
            }
        }
        return R.error().message("邮箱号已存在");
    }

    @Override
    public R ThreeZhuCe(String email, String code) {
        Long expire = redisTemplate.opsForValue().getOperations().getExpire(email);
        if (expire > 0) {
            if (code.equals(redisTemplate.opsForValue().get(email))) {
                boolean USER = beforeUserMapper.ThreeZhuCe(email);

                return R.ok().data("data", email);
            } else {
                return R.error().message("验证码错误");
            }
        } else {
            return R.error().message("验证码已过期");
        }
    }

    @Override
    public R ThreeAddUser(BeforeUser beforeUser) {
        boolean list = beforeUserMapper.ThreeAddUser(beforeUser);
        String beforeUserEmail = beforeUser.getBeforeUserEmail();
        List<BeforeUser> list1 = baseMapper.ThreeFindUserEmail(beforeUserEmail);

        return R.ok().data("data", list1);
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @Override
    public R fourQueryAll(BeforeUser beforeUser) {
        List<BeforeUser> list = beforeUserMapper.fourQueryAll(beforeUser);
        return R.ok().data("data", list);
    }

    @Override
    public R fourEditUser(BeforeUser beforeUser) {
        Integer i = beforeUserMapper.fourEditUser(beforeUser);

        return R.ok().data("i", i);
    }

    @Override
    public String selectId(int i) {
        return beforeUserMapper.selectId(i);
    }

    @Override
    public R ThreeFindId(String beforeUserId) {
        BeforeMessage beforeMessage = new BeforeMessage();
        BeforeUser s = beforeUserMapper.ThreeFindId(beforeUserId);
        beforeMessage.setUuid(beforeUserId);
        beforeMessage.setName(s.getBeforeUserName());
        beforeMessage.setAvatar(s.getBeforeUserImg());
        return R.ok().data("data", beforeMessage);
    }

    /**
     * 查看用户余额
     * @param beforeUserId
     * @return
     */
    @Override
    public BeforeUser TowBalance(Integer beforeUserId) {
        return beforeUserMapper.TowBalance(beforeUserId);
    }

    @Override
    public List<BeforeUser> zeroBeforeUserIdToBeforeUser(Integer beforeUserId) {
        return beforeUserMapper.zeroBeforeUserIdToBeforeUser(beforeUserId);
    }

    @Override
    public boolean zeroReturnGoodsSubtractBalance(Integer beforeUserId, Double returnGoodsPayment) {
        boolean b = beforeUserMapper.zeroReturnGoodsSubtractBalance(beforeUserId, returnGoodsPayment);
        if (b) {
            ZeroReturnGoods zeroReturnGoods = new ZeroReturnGoods();
            zeroReturnGoods.setBeforeUserId(beforeUserId);
            zeroReturnGoods.setReturnGoodsPayment(returnGoodsPayment);
            rabbitTemplate.convertAndSend(BeforeDirectConfig.direct_Exchange_name, BeforeDirectConfig.direct_ROUTING_KEY_name, zeroReturnGoods);

            //存消息到mq
        }
        return true;
    }

    @Override
    public boolean zeroReturnGoodsAddBalance(ZeroReturnGoods zeroReturnGoods) {
        return beforeUserMapper.zeroReturnGoodsAddBalance(zeroReturnGoods);
    }


    @Override
    public R zeroFindBeforeUserByBeforeUserId(Integer beforeUserId) {
        BeforeUser beforeUser = beforeUserMapper.zeroFindBeforeUserByBeforeUserId(beforeUserId);
        return R.ok().data("beforeUser", beforeUser);
    }

}
